<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="3" :labelWidth="110" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton v-if="type !== 'show'" :click="saveDialog" type="primary">{{ btnText }}</AjaxButton>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addRepair, updateRepair, searchLabourprice, searchDistinguish } from '@/api/repair/repair/types/index';
import { isBoolean } from 'util';

export default {
  name: 'Change',
  mixins: [dictionary],
  props: ['rowData', 'type'],
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    // this.searchprice();
    this.searchCharge();

    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      // debugger;
      if (this.type === 'edit') {
        // 更新接口需要的记录 ID
        this.recordCode = this.rowData.REPAIR_TYPE_CODE;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '维修类型代码',
          fieldName: 'REPAIR_TYPE_CODE',
          placeholder: '请输入...',
          maxlength: 8,
          disabled: this.type == 'edit' ? true : false,
          rules: [{ required: true, validator: this.checkType, message: '请输入正确维修类型代码', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '维修类型名称',
          fieldName: 'REPAIR_TYPE_NAME',
          maxlength: 12,
          placeholder: '请输入...',
          rules: [{ required: true, message: '请输入维修类型名称', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '是否保险',
          fieldName: 'IS_INSURANCE',
          placeholder: '请选择',
          // disabled:this.show(this.rowData.OEM_TAG,this.rowData.IS_INSURANCE),
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否系统保留',
          fieldName: 'IS_RESERVED',
          placeholder: '请选择',
          disabled: true,
          initialValue: 10041002,
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否售前维修',
          fieldName: 'IS_PRE_SERVICE',
          placeholder: '请选择',
          //  disabled:this.show(this.rowData.OEM_TAG,this.rowData.IS_INSURANCE),
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否钣喷',
          fieldName: 'IS_SPRAY_LABOUR',
          placeholder: '请选择',
          //  disabled:this.show(this.rowData.OEM_TAG,this.rowData.IS_INSURANCE),
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '是否有效',
          fieldName: 'IS_VALID',
          placeholder: '请选择',
          // disabled:this.show(this.rowData.OEM_TAG,this.rowData.IS_INSURANCE),
          itemList: this.createDictList('1004')
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'OEM_TAG',
          disabled: true,
          initialValue: 10451001,
          itemList: this.createDictList('1045')
        },
        {
          type: 'INPUT_NUMBER',
          label: '备件价格系数',
          fieldName: 'PART_PRICE_RATE',
          initialValue: 1,
          precision: 2,
          min: 0.0,
          max: 9.99
        },
        {
          type: 'SELECT',
          label: '备件价格类型',
          fieldName: 'PRICE_TYPE',
          itemList: this.createDictList('8050')
        },
        {
          type: 'SELECT',
          label: '收费区分',
          fieldName: 'CHARGE_PARTITION_CODE',
          itemList: [],
          rules: [{ required: true, message: '请选择收费区分', trigger: 'blur' }]
        }
      ];
    },
    // 校验维修类型代码
    checkType(rule, value, callback) {
      if (!value) {
        return callback(new Error(rule.message));
      } else {
        const reg = /^[0-9a-zA-Z]+$/;
        console.log(reg.test(value));
        if (reg.test(value)) {
          callback();
        } else {
          return callback(new Error(rule.message));
        }
      }
      callback();
    },
    async searchCharge() {
      const res = await searchDistinguish();
      this.formList.find(item => item.fieldName === 'CHARGE_PARTITION_CODE').itemList = res.data.map(x => ({ text: x.CHARGE_PARTITION_NAME, value: x.CHARGE_PARTITION_CODE }));
    },

    formChangeHandle(val) {
      this.saveData = val;
    },

    async saveDialog() {
      this.saveData = null;
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData != null) {
        let ref = {
          repairTypeCode: this.saveData.REPAIR_TYPE_CODE,
          repairTypeName: this.saveData.REPAIR_TYPE_NAME,
          labourPrice: this.saveData.LABOUR_PRICE,
          isinsurance: this.saveData.IS_INSURANCE,
          isreserved: this.saveData.IS_RESERVED,
          ispreservice: this.saveData.IS_PRE_SERVICE,
          isspraylabour: this.saveData.IS_SPRAY_LABOUR,
          // isguarantee: this.saveData.IS_GUARANTEE,
          isvalid: this.saveData.IS_VALID,
          oemtag: this.saveData.OEM_TAG,
          partPriceRate: this.saveData.PART_PRICE_RATE,
          priceType: this.saveData.PRICE_TYPE,
          chargePartitionCode: this.saveData.CHARGE_PARTITION_CODE
        };
        console.log(ref);
        let res = [];
        if (this.type === 'add') {
          res = await addRepair(ref);
        }
        if (this.type === 'edit') {
          res = await updateRepair(ref, this.recordCode);
        }
        if (res.resultCode === 200) {
          this.$notify.success({ title: '提示信息', message: '保存成功' });
          this.closeDialog('refresh');
        } else {
          return this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
        }
      } else {
        return this.$notify.warning({ title: '提示信息', message: '有校验信息未通过' });
      }
    },

    closeDialog(param) {
      this.$emit('close', false, param);
    }
  }
};
</script>

<style lang="less" scoped></style>
